package com.wayhome.srpingbootmybatis.ims.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayhome.srpingbootmybatis.ims.dao.DoubtsStatisticsDAO;
import com.wayhome.srpingbootmybatis.ims.dto.DailyPendingDoubtsDO;
import com.wayhome.srpingbootmybatis.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DoubtsStatisticsBOImpl {

    @Resource
    private DoubtsStatisticsDAO doubtsStatisticsDAO;

    public static final int SIZE = 50;

    public static final int INSERT_SIZE = 500;

    public String pendingDoubtsStatistics() {

        Date now = new Date();
        Date date = DateUtils.addDays(now, -1);
        doubtsStatisticsDAO.deleteDataForDate(date);

        List<String> docIds = doubtsStatisticsDAO.queryNeedHandleDocIds();
        if (CollectionUtils.isEmpty(docIds)) {
            log.info("未查询到需要待处理的疑点单据信息");
            return "success";
        }
        List<DailyPendingDoubtsDO> list = new ArrayList<>();
        List<String> splitDocIds  = new ArrayList<>();

        for (String docId : docIds) {
            try {
                splitDocIds.add(docId);
                if (splitDocIds.size() == SIZE) {
                    List<DailyPendingDoubtsDO> dailyPendingDoubtsDOS = doubtsStatisticsDAO.queryNeedHandleDoc(splitDocIds);
                    list.addAll(dailyPendingDoubtsDOS);
                    splitDocIds.clear();
                }
            }catch (Exception e){
                log.info("循环查询待处理疑点单据时出现错误。错误信息内容：{}", e.getMessage(), e);
            }
        }
        if (!CollectionUtils.isEmpty(splitDocIds)){
            List<DailyPendingDoubtsDO> dailyPendingDoubtsDOS = doubtsStatisticsDAO.queryNeedHandleDoc(splitDocIds);
            list.addAll(dailyPendingDoubtsDOS);
            splitDocIds.clear();
        }

        List<DailyPendingDoubtsDO> listByDr = new ArrayList<>();
        Map<String, List<DailyPendingDoubtsDO>> map = list.stream().collect(Collectors.groupingBy(DailyPendingDoubtsDO::getDrordDrCodg));

        map.forEach((key,value) -> {
            DailyPendingDoubtsDO dailyPendingDoubtsDO = new DailyPendingDoubtsDO();
            dailyPendingDoubtsDO.setDrordDrCodg(key);
            String collect = value.stream().map(DailyPendingDoubtsDO::getDocs).collect(Collectors.joining(","));
            dailyPendingDoubtsDO.setDocs(collect);
            dailyPendingDoubtsDO.setGenDate(now);
            listByDr.add(dailyPendingDoubtsDO);
            try {
                if (listByDr.size() == INSERT_SIZE) {
                    boolean result = doubtsStatisticsDAO.batchSaveDailyDocTask(listByDr);
                    listByDr.clear();
                }
            }catch (Exception e) {
                log.info("保存每日需要处理单据失败：{}", e.getMessage(), e);
            }

        });
        if (!CollectionUtils.isEmpty(listByDr)){
            boolean result = doubtsStatisticsDAO.batchSaveDailyDocTask(listByDr);
            if (result) {
                return "success";
            } else {
                return "fail";
            }
        }
        return "success";
    }
}
